﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using PMIS.BLL;
using PMIS.BLL.Post;

namespace PMIS.UI.Post
{
	/// <summary>
	/// 岗位等级信息管理
	/// </summary>
	public partial class 岗位等级信息管理 : BaseForm
	{
		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static 岗位等级信息管理 _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private 岗位等级信息管理()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static 岗位等级信息管理 GetInstance()
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new 岗位等级信息管理();
				}
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void 岗位等级信息管理_Load(object sender, System.EventArgs e)
		{
			try
			{
				cbLevel.BeginUpdate();
				cbLevel.DataSource = BL_岗位.岗位分类;
				cbLevel.SelectedIndex = cbLevel.Items.Count > 0 ? 0 : -1;
				cbLevel.EndUpdate();
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 界面显示
		/// <summary>
		/// 更新列表显示
		/// </summary>
		private void UpdateListView()
		{
			try
			{
				if (cbLevel.SelectedIndex < 0)
				{
					ShowErrorMessage("请选择岗位分类!");
					return;
				}

				//	获取分类等级
				List<岗位_分类_等级> list = Singleton<BL_岗位_分类_等级>.GetInstance().GetDetails(cbLevel.Text);

				dgvLevel.AutoGenerateColumns = false;
				dgvLevel.Rows.Clear();
				foreach (岗位_分类_等级 item in list)
				{
					dgvLevel.Rows.Add(new object[] { item.等级, item.ID });
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 等级选择
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void cbLevel_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			UpdateListView();
		}
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btAdd_Click(object sender, System.EventArgs e)
		{
			try
			{
				//	新增到最后
				DataGridViewRow dgvr = dgvLevel.Rows[dgvLevel.Rows.Add()];
				dgvLevel.FirstDisplayedCell = dgvr.Cells[0];
				dgvr.Selected = true;
				dgvr.Cells[ColumnLevel.Index].Selected = true;
				dgvLevel.BeginEdit(true);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btDelete_Click(object sender, System.EventArgs e)
		{
			if (dgvLevel.SelectedRows.Count == 0)
			{
				ShowMessageBox("请先选择要删除的记录!");
				return;
			}

			if (ShowYesNoMessage("删除所选记录吗?") != DialogResult.Yes)
				return;

			try
			{
				int intCurrentRowIndex = dgvLevel.SelectedRows[0].Index;
				dgvLevel.Rows.Remove(dgvLevel.SelectedRows[0]);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 上移
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btMoveUp_Click(object sender, System.EventArgs e)
		{
			if (dgvLevel.SelectedRows.Count == 0)
			{
				ShowMessageBox("请先选择要移动的记录!");
				return;
			}

			try
			{
				int intCurrentRowIndex = dgvLevel.SelectedRows[0].Index;
				//	判断是不是顶行
				if (intCurrentRowIndex <= 0)
					return;

				for (int index = 0; index < dgvLevel.Columns.Count; index++)
				{
					//	保存当前行的信息
					object value = dgvLevel.Rows[intCurrentRowIndex].Cells[index].Value;
					//	将上一行的值复制到本行
					dgvLevel.Rows[intCurrentRowIndex].Cells[index].Value = dgvLevel.Rows[intCurrentRowIndex - 1].Cells[index].Value;
					//	将保存的值赋值给上一行
					dgvLevel.Rows[intCurrentRowIndex - 1].Cells[index].Value = value;
				}

				//	选中上一行
				dgvLevel.Rows[intCurrentRowIndex - 1].Selected = true;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 下移
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btMoveDown_Click(object sender, System.EventArgs e)
		{
			if (dgvLevel.SelectedRows.Count == 0)
			{
				ShowMessageBox("请先选择要移动的记录!");
				return;
			}

			try
			{
				int intCurrentRowIndex = dgvLevel.SelectedRows[0].Index;
				//	判断是不是最后一行
				if (intCurrentRowIndex >= dgvLevel.Rows.Count - 1)
					return;

				for (int index = 0; index < dgvLevel.Columns.Count; index++)
				{
					//	保存当前行的信息
					object value = dgvLevel.Rows[intCurrentRowIndex].Cells[index].Value;
					//	将下一行的值复制到本行
					dgvLevel.Rows[intCurrentRowIndex].Cells[index].Value = dgvLevel.Rows[intCurrentRowIndex + 1].Cells[index].Value;
					//	将保存的值赋值给下一行
					dgvLevel.Rows[intCurrentRowIndex + 1].Cells[index].Value = value;
				}

				//	选中下一行
				dgvLevel.Rows[intCurrentRowIndex + 1].Selected = true;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 检查用户输入
		/// </summary>
		/// <returns></returns>
		private bool CheckInput()
		{
			if (cbLevel.SelectedIndex < 0)
			{
				ShowErrorMessage("请选择岗位分类!");
				cbLevel.Focus();
				return false;
			}

			if (dgvLevel.Rows.Count == 0)
			{
				ShowErrorMessage("请输入等级信息!");
				dgvLevel.Focus();
				return false;
			}

			foreach (DataGridViewRow row in dgvLevel.Rows)
			{
				if (string.IsNullOrEmpty((string)row.Cells[ColumnLevel.Index].Value))
				{
					ShowErrorMessage("请输入等级信息!");
					row.Selected = true;
					return false;
				}
			}

			return true;
		}
		/// <summary>
		/// 保存
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btSave_Click(object sender, System.EventArgs e)
		{
			if (!CheckInput())
				return;

			try
			{
				List<岗位_分类_等级> list = new List<岗位_分类_等级>();
				foreach (DataGridViewRow row in dgvLevel.Rows)
				{
					岗位_分类_等级 item = new 岗位_分类_等级();
					item.ID = string.IsNullOrEmpty((string)row.Cells[ColumnID.Index].Value) ? Guid.NewGuid().ToString("D") : (string)row.Cells[ColumnID.Index].Value;
					item.分类 = cbLevel.Text;
					item.等级 = (string)row.Cells[ColumnLevel.Index].Value;

					list.Add(item);
				}

				//	保存
				if (Singleton<BL_岗位_分类_等级>.GetInstance().Save(cbLevel.Text, list))
				{
					ShowSuccessMessage("保存成功!");
					//	更新列表
					UpdateListView();
				}
				else
				{
					ShowErrorMessage("保存失败!");
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 退出
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}
		#endregion
	}
}